﻿using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Telerik.Web.Mvc.UI;
using Telerik.Web.Mvc.UI.Fluent;

namespace WebSite.ViewModels.EditViewModels.EditGridViewModels
{
    public interface IEditGridViewModel : IHtmlString
    {
        bool ContainsTipo { get; }
        string JavascriptFunctionName { get; }
        void RenderGrid(HtmlHelper htmlHelper);
    }

    public abstract class EditGridViewModel<TItem> : IEditGridViewModel
        where TItem : EditGridItem
    {
        private readonly HtmlHelper m_HtmlHelper;
        private readonly string m_GridName;
        private readonly string m_ControllerName;
        private readonly IEnumerable<TItem> m_Models;
        private readonly string m_RouteKey;

        protected EditGridViewModel(HtmlHelper htmlHelper, string gridName, string controllerName, IEnumerable<TItem> models, string routeKey = null, bool containsTipo = true)
        {
            m_HtmlHelper = htmlHelper;
            m_GridName = gridName;
            m_ControllerName = controllerName;
            m_Models = models;
            ContainsTipo = containsTipo;
            m_RouteKey = routeKey ?? (gridName + "Id");
        }

        public bool ContainsTipo { get; private set; }
        public string JavascriptFunctionName { get { return "On" + m_GridName + "Edit"; } }

        protected abstract void Columns(GridColumnFactory<TItem> configurator);

        private IHtmlString Render(HtmlHelper helper)
        {
            return helper.Partial("EditGrid", this);
        }

        public void RenderGrid(HtmlHelper htmlHelper)
        {
            htmlHelper.Telerik().Grid(m_Models)
                .Name(m_GridName)
                .DataKeys(configurator => configurator.Add(editModel => editModel.Id).RouteKey(m_RouteKey))
                .Columns(configurator =>
                {
                    Columns(configurator);
                    configurator.Command(command =>
                    {
                        command.Edit().ButtonType(GridButtonType.Image);
                        command.Delete().ButtonType(GridButtonType.Image);
                    }).Width(78);
                })
                .ToolBar(configurator => configurator.Insert().ButtonType(GridButtonType.ImageAndText))
                .Editable(configurator => configurator.Mode(GridEditMode.PopUp).DisplayDeleteConfirmation(true))
                .DataBinding(configurator => configurator.Ajax()
                    .Select(m_GridName + "Select", m_ControllerName)
                    .Insert(m_GridName + "Insert", m_ControllerName)
                    .Delete(m_GridName + "Delete", m_ControllerName)
                    .Update(m_GridName + "Update", m_ControllerName))
                .Resizable(configurator => configurator.Columns(true))
                .ClientEvents(events => 
                {
                    if (ContainsTipo)
                    {
                        events.OnEdit(JavascriptFunctionName);
                    }
                })
                .Render();
        }

        public string ToHtmlString()
        {
            return Render(m_HtmlHelper).ToHtmlString();
        }
    }
}